package com.hos.utile;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by Administrator on 2018/8/22 0022.
 */
public class JsonUtile {
    // ----------------jackson--------------------//

    /**
     * 获取节点
     *
     * @param json
     * @return jsonNode
     */
    public static JsonNode getJsonNode(String json) {
        JsonNode jsonNode;
        try {
            ObjectMapper mapper = new ObjectMapper();
            jsonNode = mapper.readTree(json);
        } catch (IOException e) {
            jsonNode = null;
            e.printStackTrace();
        }
        return jsonNode;
    }

    /**
     * bean转string
     */
    public static String obj2Json(Object obj) {
        if (obj == null) {
            return null;
        }
        String json = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 如果json中有新增的字段并且是实体类类中不存在的，不报错
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            json = mapper.writeValueAsString(obj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * @param json
     * @param cl
     * @return
     */
    public static Object json2Obj(String json, Class<?> cl) {
        Object obj = null;
        if (json == null || "".equals(json)) {
            return obj;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
                true);

        obj = getObj(objectMapper, json, null, cl);
        return obj;
    }

    /**
     * @param json 解析源
     * @param base
     * @param bean 形如base<com.hos.operation.api.web.bean>
     * @return
     */
    public static Object json2Obj(String json, Class<?> base, Class<?> bean) {
        Object obj = null;
        if (json == null || "".equals(json)) {
            return obj;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,
                true);
        JavaType javaType = objectMapper.getTypeFactory()
                .constructParametricType(base, bean);
        obj = getObj(objectMapper, json, javaType, null);
        return obj;
    }

    /**
     * json转对象
     *
     * @param objectMapper
     * @param json         被转化的字符串
     * @param javaType     要转化的类型
     * @param cls          要转化的类型
     * @return 对象
     */
    private static Object getObj(ObjectMapper objectMapper, String json,
                                 JavaType javaType, Class<?> cls) {
// 如果json中有新增的字段并且是实体类类中不存在的，不报错
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        Object obj = null;
        try {
            if (javaType != null) {
                obj = objectMapper.readValue(json, javaType);
            }
            if (cls != null) {
                obj = objectMapper.readValue(json, cls);
            }
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return obj;
    }

}
